package com.ncmmall.mall.controller.business.freight;

import com.ncmmall.application.business.merchant.MerchantApplication;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author linxs
 * @Description: 商家配送自提设置
 * @Date: 2019-08-31 11:58
 */
@Controller
@RequestMapping(value = "/merchant/shipping")
public class ShippingSetterController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private MerchantApplication merchantApplication;

    @RequestMapping(value = "/setter", method = RequestMethod.GET)
    public String setter(Model model) {

        WebUser merchant = queryChannelService.findOne(QWebUser.webUser.id.eq(Securitys.currentUser().getId()), WebUser.class);

        model.addAttribute("merchant", merchant);
        model.addAttribute("_method", "PUT");

        return "/merchant/freight/shipping/setter";
    }

    @RequestMapping(value = "/setter", method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> create(@ModelAttribute WebUser params, Model model) {
        try {

            WebUser merchant = queryChannelService.findOne(QWebUser.webUser.eq(Securitys.currentUser()), WebUser.class);

            merchantApplication.setterShippingAddress(merchant, params.getWarehouseAddress());

        } catch (Exception ex) {
            return ImmutableMap.of("status", false, "message", "提交异常");
        }

        return ImmutableMap.of("status", true);
    }


}

